﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace HermesWinForm
{
    public partial class GetLabel : Form
    {
        Thread threadImport;

        string DownloadURL = "";

        public GetLabel()
        {
            InitializeComponent();
        }

        private void GetLabel_Load(object sender, EventArgs e)
        {
            linkLabelDownload.Enabled = false;

            string[] carrierNames = CarrierInfo.GetCarrierNames();
            for (int i = 0; i < carrierNames.Length; ++i)
            {
                cbCarriers.Items.Insert(i, carrierNames[i]);
            }

            this.btnGetLabel.Enabled = false;
        }

        private void cbCarriers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCarriers.SelectedItem.ToString() != "")
            {
                btnGetLabel.Enabled = true;
            }
            else
            {
                btnGetLabel.Enabled = false;
            }
        
        }

        private void btnGetLabel_Click(object sender, EventArgs e)
        {
            linkLabelDownload.Enabled = false;

            string message = string.Empty;

            if (cbCarriers.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Please select ther carrier.");
            }

            OrdersServiceReference.SalesOrderSoapClient orderClient = new OrdersServiceReference.SalesOrderSoapClient();
            HermesWinForm.OrdersServiceReference.UnLabelOrderInfo[] unLabelOrderInfos
                = orderClient.GetUnLabelOrder(CarrierInfo.GetCarrierID(cbCarriers.SelectedItem.ToString()));

            richtxtMessage.Text =  "共有" + unLabelOrderInfos.Length + "单需要从" + cbCarriers.SelectedItem.ToString() + "获取Label\n";

            int carrierID = CarrierInfo.GetCarrierID(cbCarriers.SelectedItem.ToString());
            HermesWinForm.OrdersServiceReference.LabelResult result = null;
            threadImport = new Thread(delegate()
            {
               
                //foreach (HermesWinForm.OrdersServiceReference.UnSyncOrderInfo temp in unSyncOrderInfos)
                {
                    StringBuilder sbMessage = new StringBuilder();
                    
                    try
                    {
                        if (carrierID == 1)
                        {
                            result = orderClient.GetEUBLabel(unLabelOrderInfos);
                        }
                        if (this.InvokeRequired)
                        {
                            this.Invoke(new MethodInvoker(delegate()
                            {
                                if (result.Message == "0")
                                {
                                    message = "获取成功!";
                                    DownloadURL = result.URL;
                                    linkLabelDownload.Enabled = true;
                                }
                                else
                                {
                                    message = result.Message;
                                    //temp.Notes = message;
                                    //ErrList.Add(temp);
                                }
                                sbMessage.AppendLine("处理结果：" + message);
                                richtxtMessage.AppendText(sbMessage.ToString());
                            }));
                        }
                    }
                    catch (System.Exception ex)
                    {
                        if (this.InvokeRequired)
                        {
                            this.Invoke(new MethodInvoker(delegate()
                            {
                                //temp.Notes = message;
                                //ErrList.Add(temp);
                                sbMessage.AppendLine("处理结果：" + ex.Message);
                                richtxtMessage.AppendText(sbMessage.ToString());
                            }));
                        }
                    }

                }

                
            });

            threadImport.Start();

        }

        private void linkLabelDownload_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(DownloadURL);
        }
    }
}
